import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';
import { isLogin } from '@/utils/auth';
import { userData, markData } from './projectJsonData';

setupMock({
  setup() {
    // Mock.XHR.prototype.withCredentials = true;

    // 用户信息
    Mock.mock(new RegExp('/api/user/info'), () => {

      if (isLogin()) {
        const role = window.localStorage.getItem('userRole') || 'admin';
        return successResponseWrap({
          name: '王立群',
          avatar:
            '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
          email: 'wangliqun@email.com',
          job: 'frontend',
          jobName: '前端艺术家',
          organization: 'Frontend',
          organizationName: '前端',
          location: 'beijing',
          locationName: '北京',
          introduction: '人潇洒，性温存',
          personalWebsite: 'https://www.arco.design',
          phone: '150****0000',
          registrationDate: '2013-05-10 12:10:00',
          accountId: '15012312300',
          certification: 1,
          role,
        });
      }
      return failResponseWrap(null, '未登录', 50008);
    });

    // 登录
    Mock.mock(new RegExp('/user/login'), (params: MockParams) => {
      const { mobile, pwd } = JSON.parse(params.body);
      if (!mobile) {
        return failResponseWrap(null, '用户名不能为空', 500);
      }
      if (!pwd) {
        return failResponseWrap(null, '密码不能为空', 500);
      }
      if (mobile === 'admin' && pwd === 'e10adc3949ba59abbe56e057f20f883e') {
        window.localStorage.setItem('userRole', 'admin');
        return successResponseWrap({
          accessToken: '12345',
          id: 5196,
          userName: 'admin',
          mobile: 'admin',
          company: {
            id: 0,
            name: '超管'
          }
        });
      }
      if (mobile === 'user' && pwd === 'user') {
        window.localStorage.setItem('userRole', 'user');
        return successResponseWrap({
          accessToken: '54321',
        });
      }
      return failResponseWrap(null, '账号或者密码错误', 50000);
    });

    // 登出
    Mock.mock(new RegExp('/user/logout'), () => {
      return successResponseWrap(null);
    });

    // 用户的服务端菜单
    Mock.mock(new RegExp('/api/user/menu'), () => {
      const menuList = [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            locale: 'menu.server.dashboard',
            requiresAuth: true,
            icon: 'icon-dashboard',
            order: 1,
          },
          children: [
            {
              path: 'workplace',
              name: 'Workplace',
              meta: {
                locale: 'menu.server.workplace',
                requiresAuth: true,
              },
            },
            {
              path: 'https://arco.design',
              name: 'arcoWebsite',
              meta: {
                locale: 'menu.arcoWebsite',
                requiresAuth: true,
              },
            },
          ],
        },
      ];
      return successResponseWrap(menuList);
    });


    // 创建用户，修改用户及修改用户状态
    Mock.mock(new RegExp('/api/user$'), (data: MockParams) => {
      // if(id !== 'admin') failResponseWrap(null, '账号或者密码错误', 500);
      if (data.type === 'POST') {
        // 创建用户
        const { userName, mobile, companyId, code } = JSON.parse(data.body || "{}");
        if (mobile) {
          if (mobile.length < 11) {
            return failResponseWrap(null, '手机号不正确', 500);
          }
          if (mobile.length > 11) {
            return failResponseWrap(null, '手机号不正确', 500);
          }
          const company = markData.filter(item => item.id === companyId);
          userData.push({
            id: userData.length,
            userName,
            mobile,
            code,
            company,
            status: 1
          })
          return successResponseWrap(null);
        }
      } else {
        const { type, ...newData } = JSON.parse(data.body || "{}");
        // 修改用户或者修改用户状态
        if (type === '1') {
          // 修改状态
          const { id, status } = newData;
          if (status) {
            let res;
            userData.map(item => {
              if (item.id === id) {
                item.status = status;
                res = successResponseWrap(null);
              }
            })
            return res;
          } else {
            return failResponseWrap(null, '修改失败', 500);
          }
        } else {
          // 修改密码
          const { id, status } = newData;
          let res;
          userData.map(item => {
            if (id == item.id) {
              if (status === 1) {
                res = successResponseWrap(null);
              } else {
                res = failResponseWrap(null, '账号未启用', 500);
              }
            }
          })
          return res;
        }
      }
      return failResponseWrap(null, '账号或者密码错误', 500);
    })

    // 忘记密码
    Mock.mock(new RegExp('/api/user/forget/pwd'), (data: MockParams) => {
      const { id, shortCode, newPwd } = JSON.parse(data.body || "{}");
      if (id === 'admin') {
        return successResponseWrap(null);
      }
      return failResponseWrap(null, '修改失败', 500);
    })
  },
});
